package org.rslai.tcedit.web;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.validator.InvalidStateException;
import org.hibernate.validator.InvalidValue;
import org.rslai.tcedit.domain.Product;
import org.rslai.tcedit.domain.ProductUser;
import org.rslai.tcedit.domain.Role;
import org.rslai.tcedit.domain.User;
import org.rslai.tcedit.domain.Version;
import org.rslai.tcedit.exception.ApplicationException;
import org.rslai.tcedit.security.SecurityUtil;
import org.rslai.tcedit.util.HttpUtil;
import org.rslai.tcedit.util.SessionUtil;
import org.rslai.tcedit.web.core.AbstractMvcController;

/**
 * 选择产品和版本
 * 
 * @author rslai
 * 
 * @spring.bean name="/selectProductVersion.jspx"
 */
public class SelectProductVersionController extends AbstractMvcController {

	@Override
	public Map<String, Object> getModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String method = request.getMethod();
		Map<String, Object> map = new HashMap<String, Object>();
		String url = HttpUtil.getString(request, "url", ""); // 取得要转向的url

		try {
			if (method.equals("GET")) {
				map.put("products", productService.query(true));
			}else if(method.equals("POST")) {
				String productID = HttpUtil.getString(request, "productID", ""); // 产品ID
				String productName = ""; // 产品名
				String versionID = HttpUtil.getString(request, "versionID", ""); // 版本ID
				String versionName = ""; // 版本名
				if (!productID.equals("")) {
					productName = ((Product) productService.query(Product.class, productID)).getName();
				}
				if (!versionID.equals("")) {
					versionName = ((Version) versionService.query(Version.class, versionID)).getName();
				}
				SessionUtil.setCurrentProductID(request, productID);
				SessionUtil.setCurrentProductName(request, productName);
				SessionUtil.setCurrentVersionID(request, versionID);
				SessionUtil.setCurrentVersionName(request, versionName);

				/**
				 * 根据选择的产品设置此用户的新角色
				 */
				List<String> roleNames = new LinkedList<String>();
				// 系统角色
				User user = (User) userService.query(SecurityUtil.getCurrentUsername());
				for(Role role : user.getRoles()) {
					roleNames.add(role.getName());
				}
				// 产品角色
				List<ProductUser> productUsers = productUserService.query(productID, user.getId());
				for(ProductUser productUser : productUsers) {
					roleNames.add(productUser.getRole().getName());
				}
				SecurityUtil.refreshUserAuths(roleNames); // 设置新的户角色

				response.sendRedirect( url );
			}
		} catch(Exception e) {
			map.put("error", convertException(e));
		}

		map.put("currentProductID", SessionUtil.getCurrentProductID(request));
		map.put("currentVersionID", SessionUtil.getCurrentVersionID(request));
		map.put("url", url);

		return map;
	}

	@Override
	public String getView(HttpServletRequest request, HttpServletResponse response) {
		return "/selectProductVersion.htm";
	}

	private String convertException(Exception e) throws Exception {
		if (e instanceof ApplicationException) {
			return e.getMessage();
		} else if (e instanceof InvalidStateException) {
			StringBuffer sb = new StringBuffer(128);
			InvalidValue[] ivs = ((InvalidStateException) e).getInvalidValues();
			for (InvalidValue iv : ivs)
				sb.append(iv.getMessage()).append("<br/>");
			return sb.toString();
		}
		throw e;
	}

}
